my @servers = fetchall_array('ntp_servers');
my @peers;

if (fetch_val('ntp_server'))
{
    @peers = type_list('ntp_server');
}
else
{
    @servers = type_list('ntp_server')
	unless @servers;
}

# default to country code NTP pool servers
unless (@servers)
{
    my $zone = do {
	local $/ = undef;
	local @ARGV = ("/etc/sysconfig/clock");
	(map /^ZONE="?([^"]*)/, split /\n/, <>)[0];
    };

    my $cc = lc do {
	local $/ = undef;
	local @ARGV = ("/usr/share/zoneinfo/zone.tab");
	(map /^(\w\w)\s+\S+\s+\Q$zone\E\b/, split /\n/, <>)[0];
    };

    @servers = map "$_.$cc.pool.ntp.org", 0..3
	if $cc;
}

@servers = grep $_ ne $hostname, @servers;
@peers = grep $_ ne $hostname, @peers;

my $conf = '';
$conf .= "server $_\n" foreach @servers;
$conf .= "peer $_\n" foreach @peers;
$conf .= "restrict default nomodify notrap nopeer".(fetch_val('ntp_server') ? "" : " noquery")."\n";
$conf .= "driftfile /var/lib/ntp/drift\n";

my $restart = 0;

text_install '/etc/ntp/step-tickers', join("", map "$_\n", @servers)
    and $restart++;

text_install '/etc/ntp.conf', $conf
    and $restart++;

# do it immediately so timestamps are accurate
command "service ntpd restart"
    if $restart;

add_class "NTP_$hostname" => {
    services => {
	ntpd => @servers || @peers ? 1 : 0,
    },
};

1;
